<!DOCTYPE html>
<html ng-app="question_answer_system" ng-controller="main">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>



    <link href="../css/MainStyle.css" rel="stylesheet">
    <link href="../css/Questions.css" rel="stylesheet">
    <link href="../css/Question.css" rel="stylesheet">
    <link href="../css/User_Edit.css" rel="stylesheet">
    <link href="../css/New_Question.css" rel="stylesheet">
    <link rel="stylesheet" href="http://cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css">
    <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="../bower_components/jquery-ui/themes/smoothness/jquery-ui.min.css" rel="stylesheet">
    <link href="../bower_components/jquery-ui/themes/smoothness/jquery-ui.css" rel="stylesheet">


    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="../bower_components/jquery-ui/jquery-ui.js"></script>
    <script src="http://libs.baidu.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <script src="../bower_components/angular/angular.min.js"></script>
    <script src="http://cdn.bootcss.com/angular.js/1.3.8/angular-animate.min.js"></script>
    <script src="http://cdn.bootcss.com/angular.js/1.3.8/angular-resource.min.js"></script>
    <script src="http://cdn.bootcss.com/angular.js/1.3.8/angular-route.min.js"></script>
    <script src="../bower_components/ng-file-upload/ng-file-upload-shim.js"></script>
    <script src="../bower_components/ng-file-upload/ng-file-upload.js"></script>
    <script src="../bower_components/angular-paginate-anything/src/paginate-anything.min.js"></script>
    <script src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>
    <link rel="stylesheet" href="http://cdn.bootcss.com/animate.css/3.2.0/animate.min.css">
    <script>
        $(function() {
            $( "#tabs" ).tabs();
        });
        angular.module("question_answer_system", ["bgf.paginateAnything", "ngRoute",  'ngSanitize','ngFileUpload'])
                .filter('to_trusted', ['$sce', function ($sce) {
                    return function (text) {
                        return $sce.trustAsHtml(text);
                    };
                }])
                .config(["$httpProvider", "$routeProvider", function ($httpProvider, $routeProvider) {
                    $httpProvider.defaults.withCredentials = true;
                    $routeProvider
                            .when("/", {
                                controller: 'Questions',
                                templateUrl: 'Questions.html'
                            })
                            .when("/rank",{
                                controller: 'Rank',
                                templateUrl: 'Rank.html'
                            })
                            .when("/approve/:mode",{
                                controller: 'Approve',
                                templateUrl: 'Approve.html'
                            })
                            .when("/user/edit",{
                                controller: 'User_Edit',
                                templateUrl: 'User_Edit.html'
                            })
                            .when("/user/info",{
                                controller: 'User_Info',
                                templateUrl: 'User_Info.html'
                            })
                            .when("/question/:id",{
                                controller: 'Question',
                                templateUrl: 'Question.html'
                            })
                            .when("/new/question",{
                                controller: 'New_Question',
                                templateUrl: 'New_Question.html'
                            })
                            .when("/societies",{
                                controller: 'Societies',
                                templateUrl: 'Societies.html'
                            })
                            .when("/my/questions",{
                                controller: 'My_Questions',
                                templateUrl: 'My_Questions.html'
                            })
                            .when("/recommend/question",{
                                controller: 'Recommend_Questions',
                                templateUrl: 'Recommend_Questions.html'
                            })
                            .when("/test",{
                                controller: 'test',
                                templateUrl: 'test.html'
                            })
                            .otherwise({redirectTo: '/'})
                }])
                .controller("Rank",function($scope,$http,$routeParams){
                       $scope.reload_ranks=function(){
                           $scope.reload=true;
                           $scope.ranks_url="http://qa.tavern.name/front/users/get_total_rank_for_angular";
                       }
                    $scope.reload_ranks();
                })
                .controller("Approve",function($scope,$http,$routeParams,$location){
                    $scope.mode3=$routeParams.mode;
                    $scope.info1={user_id:-1};
                    $scope.info2={user_id:-1};
                    $scope.approve1=function(){
                        $scope.mode3=1;
                    }
                    $scope.approve2=function(){
                        $scope.mode3=2;
                    }
                    $scope.approve3=function(){
                        $scope.mode3=3;
                    }
                    $scope.user_approve_submit=function(){
                        $scope.info1.user_id=$scope.user.id;
                        $http.post("http://qa.tavern.name/front/users/user_approve",$scope.info1)
                                .success(function(user){
                                    $scope.user=user;
                                    $location.path("/");
                                })
                    }
                    $scope.email_approve_submit=function(){
                        $scope.info2.user_id=$scope.user.id;
                        $http.get("http://qa.tavern.name/front/users/school_email_approve?user_id="+$scope.user.id+"&schoole_email="
                        +$scope.info2.school_email)
                                .success(function(user){
                                    $scope.user=user;
                                    $location.path("/");
                                })
                    }

                })
                .controller("test", function ($scope, Upload, $timeout) {
                    $scope.$watch('files', function () {
                        $scope.upload($scope.files);
                    });
                    $scope.log = '';

                    $scope.upload = function (files) {
                        if (files && files.length) {
                            for (var i = 0; i < files.length; i++) {
                                var file = files[i];
                                Upload.upload({
                                    url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
                                    fields: {
                                        'username': $scope.username
                                    },
                                    file: file
                                }).progress(function (evt) {
                                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                                    $scope.log = 'progress: ' + progressPercentage + '% ' +
                                    evt.config.file.name + '\n' + $scope.log;
                                }).success(function (data, status, headers, config) {
                                    $timeout(function() {
                                        $scope.log = 'file: ' + config.file.name + ', Response: ' + JSON.stringify(data) + '\n' + $scope.log;
                                    });
                                });
                            }
                        }
                    };
                })
                .controller("User_Edit",function($scope,$http,$routeParams,$location){
                    //访问404报错！
                  $scope.user_edit=$scope.user;
                  $scope.userinfo_edit=function(){
                      $http.post("http://qa.tavern.name/front/users/update?user_id="+$scope.user_edit.id,$scope.user_edit)
                              .success(function(user){
                                  $scope.user=user;
                                  $location.path("#/user/info");
                              })
                  }
                    $scope.$watch('files', function () {
                        $scope.upload($scope.files);
                    });
                    $scope.log = '';

                    $scope.upload = function (files) {
                        if (files && files.length) {
                            for (var i = 0; i < files.length; i++) {
                                var file = files[i];
                                Upload.upload({
                                    url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
                                    fields: {
                                        'username': $scope.username
                                    },
                                    file: file
                                }).progress(function (evt) {
                                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                                    $scope.log = 'progress: ' + progressPercentage + '% ' +
                                    evt.config.file.name + '\n' + $scope.log;
                                }).success(function (data, status, headers, config) {
                                    $timeout(function() {
                                        $scope.log = 'file: ' + config.file.name + ', Response: ' + JSON.stringify(data) + '\n' + $scope.log;
                                    });
                                });
                            }
                        }
                    };
                })
                .controller("User_Info",function($scope,$http,$routeParams){

                })
                .controller("Recommend_Questions",function($scope,$http,$routeParams){
                    $scope.mode1=0;
                    $scope.load_major_questions=function(){
                        $scope.mode1=0;
                    }
                    $scope.load_fellow_questions=function(){
                        $scope.mode1=1;
                    }
                    $scope.reload_recommend_questions=function(){
                        $scope.reload=true;
                        $scope.questions_url="http://qa.tavern.name/front/questions/recommend_info_for_angular?user_id="
                        +$scope.user.id+"&answer_id="+$scope.mode1;
                        console.log("11");
                    }
                    $scope.reload_recommend_questions();
                    $scope.$watch("mode1",function(){
                        $scope.reload_recommend_questions();
                    })
                })
                .controller("My_Questions",function($scope,$http,$routeParams){
                    $scope.mode2=0;
                    $scope.load_answered_questions=function(){
                        $scope.mode2=0;
                    }
                    $scope.load_submitted_questions=function(){
                        $scope.mode2=1;
                    }
                    $scope.reload_my_questions=function(){
                        $scope.reload=true;
                        if($scope.mode2==0)
                        $scope.my_question_url="http://qa.tavern.name/front/answers/user_already_answer_for_angular?user_id="
                        +$scope.user.id;
                        else
                            $scope.my_question_url="http://qa.tavern.name/front/questions/user_already_ask?user_id="
                            +$scope.user.id;
                    }
                    $scope.reload_my_questions();
                    $scope.$watch("mode2",function(){
                        $scope.reload_my_questions();
                    })
                })
                .controller("Societies",function($scope,$http,$routeParams){
                    $scope.reload_societies=function(){
                        $scope.reload=true;
                        $scope.societies_url="http://qa.tavern.name/front/societies";
                    }
                    $scope.reload_societies("http://qa.tavern.name/front/societies");
                })
                .controller("Question",function($scope,$http,$routeParams){
                    $scope.question_id = $routeParams.id;
                    $scope.new_answer={content:"",praise_num:0};
                    $scope.new_answer.user_id=$scope.user.id;
                    $scope.new_answer.user_role=$scope.user.username;
                    $scope.praise_able="no";
                        $scope.get_question=function(id){
                            $http.get("http://qa.tavern.name/front/questions/"+id)
                                    .success(function(question){
                                         $scope.question=question;
                                        $scope.new_answer.question_id=question.id;
                                    })
                        }
                    $scope.get_question( $scope.question_id);
                    $scope.reload_answers=function(id){
                        $scope.reload=true;
                        $scope.answer_url="http://qa.tavern.name/front/answers/index_for_angular?question_id="+id;
                    }
                    $scope.reload_answers($scope.question_id);
                    $scope.answer_submit=function(){
                        $http.post("http://qa.tavern.name/front/answers",$scope.new_answer)
                                .success(function(answer){
                                    $scope.new_answer.content="";
                                    $scope.new_answer.praise_num="";
                                    $scope.reload_answers($scope.question_id);
                                })
                    }
                    $scope.answer_praise=function(id){
                        if($scope.praise_able=="yes") {
                            $http.post("http://qa.tavern.name/front/answers/praise", id)
                                    .success(function (msg) {
                                        console.log(msg);
                                    })
                        }
                        else{
                                console.log("不可以点赞");
                        }
                    }
                    $scope.answer_judge_praise=function(answer_id){
                        $scope.postdata={};
                        $scope.postdata.answer_id=answer_id;
                        $scope.postdata.user_id=$scope.user.id;
                        console.log($scope.postdata);
                        $http.post("http://qa.tavern.name/front/association/judge_praise",$scope.postdata)
                                .success(function(msg){
                                    console.log(msg);
                                    $scope.praise_able=msg.message;
                                    $scope.answer_praise(answer_id);
                                })
                    }
                })
                .controller("Questions",function($scope,$http,$routeParams)
                {

                })
                .controller("New_Question",function($scope,$http,$routeParams,$location)
                {
                    $scope.new_question={title:"",content:""};
                    $scope.question_submit=function(){
                        $scope.new_question.user_id=$scope.user.id;
                        $http.post("http://qa.tavern.name/front/questions",$scope.new_question)
                                .success(function(question){
                                    $scope.new_question={title:"",content:""};
                                    $location.path("/");
                                })
                        }
                })
                .controller("main",function($scope,$http,$routeParams){
                    $scope.provinces=["澳门","香港","台湾","安徽省", "北京市", "福建省", "甘肃省", "广东省", "广西省", "贵州省", "海南省", "河北省", "河南省", "黑龙江省", "湖北省", "湖南省", "吉林省", "江苏省", "江西省", "辽宁省", "内蒙古自治区", "宁夏回族自治区", "青海省", "山东省", "山西省", "陕西省", "上海市", "四川省", "天津市", "西藏自治区", " 新疆维吾尔自治区", "云南省", "浙江省", "重庆市"]
                    $scope.search="";
                    $scope.show_dialog=false;
                    $scope.show_register=false;
                    $scope.user_login={login:"",password:""};
                    $scope.user_register={username:"",password:"",email:"",fellow_name:"重庆市"};
                    $scope.user={};
                    $scope.mode=0;
                    $scope.is_login=false;
                    $scope.get_current_user=function(){
                        $http.get("http://qa.tavern.name/front/users/current")
                                .success(function(user){
                                    console.log(user);
                                    if(user)
                                    {
                                        $scope.is_login=true;
                                        $scope.user=user;
                                    }
                                    else {
                                        $scope.is_login = false;
                                    }
                                })
                    }
                    $scope.get_current_user();
                    $scope.login = function(){
                        $http.post("http://qa.tavern.name/front/users/sign_in_for_web",$scope.user_login)
                                .success(function(user){
                                    $scope.user=user;
                                    $scope.user_login={login:"",password:""};
                                    $scope.close_dialog();
                                    $scope.is_login=true;
                                    console.log("登录成功!");
                                    console.log(user);
                                })
                    }
                    $scope.logout=function(){
                        $http.post("http://qa.tavern.name/front/users/sign_out_for_web")
                                .success(function(user){
                                    $scope.user={};
                                    $scope.close_dialog();
                                    $scope.is_login=false;
                                    console.log("注销成功!");
                                    $location.path("/");
                                })
                    }
                    $scope.register = function(){
                        $http.post("http://qa.tavern.name/front/users/sign_up",$scope.user_register)
                                .success(function(user){
                                    $scope.user=user;
                                    $scope.user_register={username:"",password:"",email:""};
                                    $scope.close_dialog();
                                }
                        )
                    }

                    $scope.show_register_dialog=function(){
                        $scope.show_dialog=true;
                        $scope.show_register=true;
                    }
                    $scope.show_login_dialog=function(){
                        $scope.show_dialog=true;
                        $scope.show_register=false;
                    }
                    $scope.close_dialog=function(){
                        $scope.show_dialog=false;
                    }
                    $scope.load_hot_questions=function(){
                        $scope.mode=1;
                    }
                    $scope.load_new_questions=function(){
                        $scope.mode=0;
                    }
                    $scope.reload_questions = function () {
                        $scope.reload = true;
                        if($scope.search=="") {
                            if ($scope.mode == 0)
                                $scope.question_url = "http://qa.tavern.name/front/questions/index_for_angular";
                            //热门排序有问题!
                            else if ($scope.mode == 1)
                                $scope.question_url = "http://qa.tavern.name/front/questions/question_hottest_for_angular";
                        }
                        else{
                            $scope.question_url="http://qa.tavern.name/front/questions/index_for_angular?title="+$scope.search;
                            console.log($scope.question_url);
                        }
                    }
                    $scope.load_ranks=function(){
                        $http.get("http://qa.tavern.name/front/users/get_total_rank")
                                .success(function(ranks){
                                    $scope.ranks=ranks;
                                })
                    }
                    $scope.load_ranks();
                    $scope.get_rank_for_user=function(){
                        $http.get("http://qa.tavern.name/front/users/get_rank")
                                .success(function(rank){
                                    $scope.rank=rank;
                                })
                    }
                    $scope.$watch("mode",function(){
                        $scope.reload_questions();
                    });
                })
    </script>

</head>
<body>
<div id="header">
    <div class="center">
        <div class="inline">
                <input type="text" placeholder="请输入你要搜索的问题" ng-model="search" id="searchinput">
                <button ng-click="reload_questions()" class="btn btn-primary" style="display: inline"><i class="fa fa-search"></i></button>
                <a class="btn btn-primary" href="#/new/question"><i class="fa fa-question-circle"></i>我要提问</a>
        </div>
        <div class="dropdown inline user" ng-show="is_login">
            <button type="button" class="btn dropdown-toggle" id="dropdownMenu2"
                    data-toggle="dropdown" style="width:158px">
                <i class="fa fa-user"></i>{{user.username}}你好！
            </button>
            <ul class="dropdown-menu " role="menu" aria-labelledby="dropdownMenu2">
                <li role="presentation">
                    <a role="menuitem" tabindex="-1"  href="#/user/edit" >编辑用户</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1"  href="#/user/info" >查看用户</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" ng-click="logout()">注销</a>
                </li>
            </ul>
        </div>
        <div class="dropdown inline user" ng-hide="is_login">
            <button type="button" class="btn dropdown-toggle" id="dropdownMenu1"
                    data-toggle="dropdown" style="width:158px">
                <i class="fa fa-user"></i>您还没有登录！
            </button>
            <ul class="dropdown-menu " role="menu" aria-labelledby="dropdownMenu1">
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" ng-click="show_register_dialog()" >注册</a></li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1"  ng-click="show_login_dialog()" >登录</a></li>
                </li>
                <li role="presentation" class="divider"></li>
            </ul>
        </div>
       <!-- <div class="login_register" >
            <ul class="list-inline" class="navigation">
                <li><a class="btn btn-primary" href="#/" class="link"><i class="fa fa-user"></i>首页</a></li>
                <li><a class="btn btn-primary" href="#/recommend/question" class="link" ng-show="is_login">推荐</a></li>
                <li ng-show="is_login"><a class="btn btn-primary" href="#/my/questions" class="link">我的问题</a></li>
            </ul>
        </div>!-->
    </div>
</div>
<div id="wrap">
    <div id="body">
        <div class="left_body" >
            <div>
                <ul class="nav nav-tabs">
                    <li><a class="active" href="#/" ><i class="fa fa-user"></i>首页</a></li>
                    <li ng-show="is_login"><a  href="#/recommend/question" >推荐</a></li>
                    <li ng-show="is_login"><a  href="#/my/questions">我的问题</a></li>
                </ul>
                <div ng-view>
                </div>
            </div>

        </div>
        <div class="right_body">
            <div class="info" ng-hide="is_login">
               <span>
                   校园问答系统可以为你解决各种疑难问题，在这里你可以找到你要的答案。
               </span>
                <br/>
                <a href="" style="color: red;" ng-click="show_login_dialog">加入我们»</a>
                    <button class="btn btn-primary btn-block" ng-click="show_login_dialog()">邮箱登录</button>
            </div>
            <div class="info" ng-show="is_login">
               <span>
                   尊敬的用户你还没有进行相关认证，快来认证吧!
               </span>
                <br/>
                <a class="btn btn-primary btn-block" href="#/approve/1">用户认证</a>
                <a class="btn btn-primary btn-block" href="#/approve/2">邮箱认证</a>
                <a class="btn btn-primary btn-block" href="#/approve/3">管理员认证</a>
            </div>
            <div class="info" ng-show="is_login">
               <span>
                   来加入你感兴趣的协会吧，大学打鸡血嗨起来!
               </span>
                <br/>
                <a href="" style="color: red;">加入我们»</a>
                <a class="btn btn-danger btn-block" href="#/societies">加入协会</a>
            </div>
            <div class="info">
               <span>
                   用户排行榜
               </span>
                <br/>
                <div>
                    <table class="table table-condensed table-hover" style="text-align: center;">
                        <thead>
                        <tr>
                            <th style="text-align: center;">名次</th>
                            <th style="text-align: center;">用户名称</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr ng-repeat="rank in ranks">
                            <td>{{$index+1}}</td>
                            <td>{{rank.username}}</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <a href="#/rank" style="color: red;">详细信息»</a>
            </div>
        </div>
    </div>
    <div id="footer">
    </div>
</div>
<div class="login_register_box" ng-show="show_dialog">
    <div class="box_title">
        <div class="title">
            加入我们
        </div>
    </div>
    <form name="RegisterForm" ng-show="show_register" novalidate>
        <div class="input">
            <input type="text" name="user" ng-model="user_register.username"  placeholder="用户名" required>
            <span style="color:red" ng-show="RegisterForm.user.$dirty && RegisterForm.user.$invalid">
                      <span ng-show="RegisterForm.user.$error.required">用户名不可为空！.</span>
                      </span>
        </div>
        <div class="input">
            <input type="password" name="password" ng-model="user_register.password" placeholder="密码" required>
                      <span style="color:red" ng-show="RegisterForm.password.$dirty && RegisterForm.password.$invalid">
                      <span ng-show="RegisterForm.password.$error.required">密码不可为空！.</span>
                      </span>
        </div>
        <div class="input">
            <input type="email" name="email" ng-model="user_register.email" placeholder="邮箱"required>
                      <span style="color:red" ng-show="RegisterForm.email.$dirty && RegisterForm.email.$invalid">
                      <span ng-show="RegisterForm.email.$error.required">邮箱不可为空！</span>
                      <span ng-show="RegisterForm.email.$error.email">不合法的邮箱类型！</span>
                      </span>
        </div>
        <div class="input">
            <select ng-model="user_register.fellow_name" style="color:black;margin-top:10px">
                <option value="全部" selected>全部</option>
                <option ng-repeat="i in provinces">{{i}}</option>
            </select>
        </div>
        <div class="box_btn">
           <input type="button" class="btn btn-primary" value="注册" ng-click="register()">
            <input type="button" class="btn btn-primary" value="取消" ng-click="close_dialog()" style="margin-left: 50px;">
        </div>
    </form >
    <form name="LoginForm" ng-hide="show_register" novalidate >
        <div class="input">
            <input type="text" name="user" ng-model="user_login.login" placeholder="邮箱" required>
                          <span style="color:red" ng-show="LoginForm.user.$dirty && LoginForm.user.$invalid">
                          <span ng-show="LoginForm.user.$error.required">用户名不可为空！.</span>
                          </span>
        </div>
        <div class="input">
            <input type="text" name="password" ng-model="user_login.password" placeholder="密码"required>
                          <span style="color:red" ng-show="LoginForm.password.$dirty && LoginForm.password.$invalid">
                          <span ng-show="LoginForm.password.$error.required">密码不可为空！.</span>
                          </span>
        </div>
        <div class="box_btn">
            <input type="button" class="btn btn-primary" value="登录" ng-click="login()">
            <input type="button" class="btn btn-primary" value="取消" ng-click="close_dialog()" style="margin-left: 50px;">
        </div>
    </form>
</div>


</body>
</html>